<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <!-- 分别通过methods和computed来计算书籍的总价 -->
    <h2>methods计算总价：{{getTotalPrice()}}</h2>
    <h2>computed计算总价：{{totalPrice}}</h2>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        books: [
          { id: 100, name: 'Vue入门', price: 110},
          { id: 101, name: '前端基础', price: 100},
          { id: 102, name: 'iOS开发', price: 90},
          { id: 103, name: '安卓开发', price: 100 },
        ]
      },
      methods: {
        getTotalPrice(){
          let result = 0
          for (let i = 0; i < this.books.length; i++) {
            result += this.books[i].price
          }
          return result
        },
        
      },
      computed: {
        totalPrice(){
          let result = 0
          // 普通的for循环
          // for (let i = 0; i < this.books.length; i++) {
          //   result += this.books[i].price
          // }
          // return result

          // let ... of ... 循环
          for(let book of this.books){
            result += book.price
          }
          return result
        }
      },
  })
  </script>
</body>
</html>